package com.north.light.libmediapreview.ui.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.north.light.libmediapreview.PreviewParams;
import com.north.light.libmediapreview.R;
import com.north.light.libmediapreview.constant.PreviewIntentCode;

/**
 * author:li
 * date:2022/9/25
 * desc:预览基类
 */
public abstract class PreviewBaseActivity extends FragmentActivity {

    private PreviewParams mIntentData;
    private Handler mUIHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIntent();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            if (statusBarColorRes() != 0) {
                //顶部状态栏
                window.setStatusBarColor(getResources().getColor(statusBarColorRes()));
                //底部导航栏
                window.setNavigationBarColor(getResources().getColor(statusBarColorRes()));
            }
        }
        setContentView(getLayoutId());
        overridePendingTransition(R.anim.lib_media_preview_anim_alpha_in, R.anim.lib_media_preview_anim_alpha_out);
        preInitView();
        initData();
        mUIHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                switch (screenOrg()) {
                    case 0:
                        try {
                            //竖屏
                            if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT) {
                                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case 1:
                        try {
                            //横屏
                            if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
                                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        }, 300);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(isFinishing()){
            mUIHandler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.lib_media_preview_anim_alpha_in, R.anim.lib_media_preview_anim_alpha_out);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initIntent();
    }

    private void initIntent() {
        if (getIntent() != null) {
            if (getIntent().getSerializableExtra(PreviewIntentCode.INTENT_PREVIEW_OBJ) instanceof PreviewParams) {
                mIntentData = (PreviewParams) getIntent().getSerializableExtra(PreviewIntentCode.INTENT_PREVIEW_OBJ);
            }
        }
        if (mIntentData == null) {
            finish();
        }
    }

    /**
     * 设置全屏
     */
    public void setFullScreen() {
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 取消全屏
     */
    public void cancelFullScreen() {
        getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 能否点击图片外部取消
     */
    protected boolean canCancelOutSide() {
        if (getIntentData() == null) {
            return false;
        }
        return getIntentData().isPreviewCancelOutSide();
    }

    protected PreviewParams getIntentData() {
        return mIntentData;
    }

    //屏幕方向 0竖屏 1横屏
    protected abstract int screenOrg();

    //状态栏颜色
    protected abstract int statusBarColorRes();

    protected abstract int getLayoutId();

    protected abstract void initData();

    protected abstract void preInitView();
}
